package personal.wang.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Objects;
import java.util.Properties;

/**
 * @className: JDBCUtil
 * @Description:
 * @Author: wangyifei
 * @Date: 2022/6/16 21:18
 */
public class JDBCUtil {
    private static Logger logger = LoggerFactory.getLogger(JDBCUtil.class);
    private static String driverClass ;
    private static String url ;
    private static String username ;
    private static String password;
    private static String[] EXECUTOR = {"truncate\\s+table",
            "delete\\s+from",
            "insert\\s+into",
            "create\\s+table",
    };

    private static void readConfig() {
        InputStream inputStream = null ;
        try{
            inputStream = JDBCUtil.class.getResourceAsStream("/DBconfig.properties");
            Properties prop = new Properties();
            prop.load(inputStream);
            driverClass = prop.getProperty("driverClassName");
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(!Objects.isNull(inputStream)){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    static {
        try{
            readConfig();
            Class.forName(driverClass);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static Connection getConnection() {
        try{
            return DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null ;
    }

    public static void execute(String sql){
        Connection connection = getConnection();
        Statement stmt = null;
        try {
            stmt = connection.createStatement();
            boolean execute = stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(stmt,connection);
        }
    }

    public static String query(String sql){
        String rs = null ;
        Connection connection = getConnection();
        Statement statement = null ;
        ResultSet resultSet = null ;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            while(resultSet.next()){
                rs = resultSet.getString(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(resultSet ,statement , connection);
        }
        return rs ;
    }

    private static void close(Statement stmt, Connection connection) {
        close(null , stmt , connection);
    }
    private static void close(ResultSet rs , Statement stmt , Connection connection){
        if(!Objects.isNull(rs)) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(!Objects.isNull(stmt)){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(!Objects.isNull(connection)){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
